import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface TutorialStep {
  [key: string]: any;
}

export interface TutorialsState {
  steps: TutorialStep[];
  tourStep: number;
  tourRunning: boolean;
  requestedTourStep: number | null;
  lastCheckRender: any;
}

const initialState: TutorialsState = {
  steps: [],
  tourStep: 0,
  tourRunning: false,
  requestedTourStep: null,
  lastCheckRender: null,
};

const tutorialsSlice = createSlice({
  name: 'tutorial',
  initialState,
  reducers: {
    setSteps: (state, action: PayloadAction<TutorialStep[]>) => {
      state.steps = action.payload;
    },
    setTourStep: (state, action: PayloadAction<number>) => {
      state.tourStep = action.payload;
    },
    setTourRunning: (state, action: PayloadAction<boolean>) => {
      state.tourRunning = action.payload;
    },
    setRequestedTourStep: (state, action: PayloadAction<number | null>) => {
      state.requestedTourStep = action.payload;
    },
    setLastCheckRender: (state, action: PayloadAction<any>) => {
      state.lastCheckRender = action.payload;
    },
    startTutorial: (state) => {
      state.tourRunning = true;
    },
    stopTutorial: (state) => {
      state.tourRunning = false;
      state.tourStep = 0;
      state.requestedTourStep = null;
    },
    incrementTutorialStep: (state) => {
      state.tourStep += 1;
    },
    runControlledStep: (state) => {
      // Controlled step logic
    },
    validateTutorialStep: (state, action: PayloadAction<{ tourStep: number }>) => {
      // Validation logic
    },
    addDiscoveredStep: (state, action: PayloadAction<any>) => {
      // Add discovered step logic
    },
    runControlledStepByElementId: (state, action: PayloadAction<any>) => {
      // Run controlled step by element ID
    },
    checkBubbleRender: (state, action: PayloadAction<any>) => {
      state.lastCheckRender = action.payload;
    },
  },
});

export const {
  setSteps,
  setTourStep,
  setTourRunning,
  setRequestedTourStep,
  setLastCheckRender,
  startTutorial,
  stopTutorial,
  incrementTutorialStep,
  runControlledStep,
  validateTutorialStep,
  addDiscoveredStep,
  runControlledStepByElementId,
  checkBubbleRender,
} = tutorialsSlice.actions;

export default tutorialsSlice.reducer;

